package com.example.demo.controller;

import com.example.demo.model.Contact;
import com.example.demo.service.ContactService;
import com.example.demo.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 处理意见建议的相关请求参数
 * User: su
 * Date: 2023-04-08
 * Time: 14:21
 */
@RestController
@RequestMapping("/contact")
public class ContactController {

    // 注入 ContactService 依赖
    @Autowired
    private ContactService contactService;

    /**
     * 处理前端参数，并调用 ContactService
     * @param name 名字
     * @param email 邮箱
     * @param subject 标题
     * @param message 内容
     * @return 返回 {code: 200/400, message: "成功"/"失败", data: true/false;}
     */
    @RequestMapping("/add")
    public ResponseBodyMessage<Boolean> add (@RequestParam("name") String name,
                                                    @RequestParam("email") String email,
                                                    @RequestParam("subject") String subject,
                                                    @RequestParam("message") String message
    ) {
        // 非空判断
        if (StringUtils.hasLength(name) ||
                StringUtils.hasLength(email) ||
                StringUtils.hasLength(subject) ||
                StringUtils.hasLength(message)
        ) {
            // 调用 contactService.add() 处理请求
            return contactService.add(name, email, subject, message);
        } else {
            // 参数错误
            return new ResponseBodyMessage<>(400, "请求参数错误",false);
        }
    }


    @RequestMapping("/getAll")
    public ResponseBodyMessage<List<Contact>> getAll () {
        //调用 contactService.getAll() 处理请求
        return contactService.getAll();
    }

    @RequestMapping("/getPage")
    public ResponseBodyMessage<List<Contact>> getPage (@RequestParam ("pageIndex")Integer pageIndex,
                                                                   @RequestParam("pageSize") Integer pageSize
    ) {
        // 非空判断
        if (pageIndex != null && pageSize != null) {
            // 调用 contactService.getPage() 处理请求
            return contactService.getPage(pageIndex,pageSize);
        } else {
            // 参数错误
            return new ResponseBodyMessage<>(400, "请求参数错误",null);
        }

    }

    @RequestMapping("/get")
    public ResponseBodyMessage<Contact> get (@RequestParam ("id")Integer id) {
        // 非空判断
        if (id != null ) {
            // 调用 contactService.get() 处理请求
            return contactService.get(id);
        } else {
            // 参数错误
            return new ResponseBodyMessage<>(400, "请求参数错误",null);
        }
    }

    @RequestMapping("/delete")
    public ResponseBodyMessage<Boolean> delete (@RequestParam ("id")Integer id) {
        // 非空判断
        if (id != null ) {
            // 调用 contactService.delete() 处理请求
            return contactService.delete(id);
        } else {
            // 参数错误
            return new ResponseBodyMessage<>(400, "请求参数错误",false);
        }
    }
}
